﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using System.Xml;
using Aulac.Global.Enums;
using Aulac.Global.Data;
using Aulac.Global.Common;
using Aulac.Global.Xml;
using Aulac.Client.Data.Helper;
using Aulac.Verb.Ems;
using Aulac.Global.AccessRight;
using Aulac.Rsc;
using Aulac.Client.Global.Interface;
using Aulac.Client.Ems.WinForm.Store.DataFiles;
using Aulac.Rsc.Bss.Tags;
using Aulac.Rsc.Bss;
using Aulac.Verb.Bss;

namespace Aulac.Client.Bss.InOutPut
{
    partial class FormTabDepr
    {
        //private bool mIsCheckInstock = false;

        #region Override Methods 
        public override void OnFormLoad()
        {
            try
            {                
                base.OnFormLoad();
                InitGrd();                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
       
        protected override bool OnShowDetailVoucher()
        {
            if (ValueHelper.isEmpty(grdInfo.GetValue(grdInfo.SelectedGridRowView, tagTab.TabHeaderDepr.TabDeprPrkID)) && (grdInfo.RowCount > 0))
            {
                Aulac.UtilityTools.MsgBox.Show("Vui lòng chọn một dòng ở [Danh sách] để hiển thị chi tiết !", Aulac.UtilityTools.MsgBox.Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            else if (ValueHelper.isEmpty(grdInfo.GetValue(grdInfo.SelectedGridRowView, tagTab.TabHeaderDepr.TabDeprPrkID))) return false;
            return GetDetailTabDepr();
        }

        protected override void OnAddNewVoucher()
        {
            this.grdLine.ClearDataContainer();
            //if (ValueHelper.isEmpty(mVchName))
            //    mVchName = Aulac.Client.Data.Ems.Helper.VchHelper.GetVchNameOnServer(mVchType);
            this.Text = mVchName;
            base.OnAddNewVoucher();
        }

        protected override void OnEditVoucher()
        {
            //mIsCheckInstock = true;
            base.OnEditVoucher();
        }

        protected override bool OnBeginSaveVoucher()
        {
            grdLine.grdData.EndEditingRow(false);
            //grdLine.grdData.RemoveAllRowsByColEmpty(tagItems.Items.ItemID, tagVch.VchBuyLineCapAss.Quantity);
            
            if (grdLine.grdData.Rows.Count == 0)
            {
                //Aulac.UtilityTools.AulacMsg.Show(Aulac.Client.Ems.WinForm.Stores.Lang.AulacLangMsg.GetMsg(Aulac.Client.Ems.WinForm.Stores.Lang.AulacCodes.ErrAlEmsCMS003));
                grdLine.Focus();
                return false;
            }
            int iCount = grdLine.ReadOnly == true ? grdLine.grdData.Rows.Count : grdLine.grdData.Rows.Count - 1;
            //for (int i = 1; i < iCount; i++)
            //{
        //        int mQuantity = (int)ValueHelper.ChangeType(grdLine.grdData.GetValueDataCell(i, grdLine.ColumnsGrd.Quantity), typeof(int));
                //string sItemID =(string)ValueHelper.ChangeType(grdLine.grdData.GetValueDataCell(i, grdLine.ColumnsGrd.ItemID), typeof(string));
        //        if (mQuantity == 0 || sItemID == null)
        //        {
        //            if (mQuantity == 0)
        //                Aulac.UtilityTools.MsgBox.Show(string.Format("Số lượng dòng [ {0} ] không được bằng không !", i), Aulac.UtilityTools.MsgBox.Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
        //            else
        //                Aulac.UtilityTools.MsgBox.Show(string.Format("Mã y tế ở dòng [ {0} ] đang rỗng !", i), Aulac.UtilityTools.MsgBox.Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
        //            grdLine.grdData.SelectedIndexGridRow = i;
        //            return false;
        //        }                
        //    }
          return true;
        }

        protected override bool OnSaveVoucherData(ref XmlDataDocument xmlData)
        {
            return SaveTabDepr(ref xmlData);
        }

        protected override bool OnDeleteDataVoucher()
        {
            return DelTabDepr();
        }

        protected override void OnSetStateControls(FormModes pFormMode)
        {
            //gridBssDepr1.Enabled = true;
            //gridBssDepr1.ReadOnly = false;
            base.OnSetStateControls(pFormMode);
            bool isReadOnly = false;
            if (pFormMode == FormModes.NewMode || pFormMode == FormModes.EditMode)
                isReadOnly = false;
            else
                isReadOnly = true;
            foreach (Aulac.Client.Global.Interface.IAulacStateCtrl ctr in GetListControlsOnForm())
            {
                ctr.FormMode = pFormMode;
                ctr.Enabled = !isReadOnly;
                ctr.ReadOnly = isReadOnly;
            }
           grdLine.FormMode = pFormMode;
        }

        protected override void OnClearValueControls()
        {
            base.OnClearValueControls();
            foreach (Aulac.Client.Global.Interface.IAulacValueDataCtrl ctr in GetListControlsOnForm())
                ctr.ClearValueControls();
            grdLine.ClearDataContainer();
           // grdLine.ClearValueTtAmt();
        }

        protected override void OnSetAccessRight(AccessRights accRight)
        {
            base.OnSetAccessRight(accRight);
        }

        protected override void OnRefreshList()
        {
            try
            {
                XmlDataDocument xmlDoc = CreateXmlDocument(AulacBssVerb.TabDepr.GetListTabDepr, AulacBssVerb.TabDepr.Noun.TabDepr);
                XmlProcess.AddValueParamItem(xmlDoc, tagGeneral.TabFromDate, this.dtVchFromDate.GetValue());
                XmlProcess.AddValueParamItem(xmlDoc, tagGeneral.TabToDate, this.dtVchToDate.GetValue());
                grdInfo.ClearDataContainer();
                grdInfo.ReadOnly = true;
                DataHelper.GetDataFromServerToControl(grdInfo, xmlDoc);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        protected override void OnPrintVoucher()
        {
            Aulac.Client.Reports.Ems.VchPrints.RptVchPrint.ShowReport(GetValueToXml(), DataFileInfos.VchRecStoreName);
        }
        #endregion

        #region Private Methods
        private void Init()
        {
            this.FieldVchPrkID = tagTab.TabHeaderDepr.TabDeprPrkID;
            //this.mVchType = VchTypes.VchBuyItem;
            //this.grdLine.LstMdnGrpTypes = new List<ItemGrpTypes> { ItemGrpTypes.DESKTOP, ItemGrpTypes.Camera };
            grdInfo.DataSource = new Aulac.Client.Data.Bss.DataSet.DsTabBss.TableHeaderDepreciationDataTable();
        }
        private void InitGrd()
        {
            grdInfo.ClearColumsShow();
            grdInfo.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Số", ColName = tagTab.TabHeaderDepr.TabNo, DisplayIndex = 0 });
            grdInfo.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Ngày", ColName = tagTab.TabHeaderDepr.TabDate, DisplayIndex = 1 });
            grdInfo.AddColumnShow(new Aulac.UtilityTools.ColumnGridViewInfo() { ColCaption = "Diễn giải", ColName = tagTab.TabHeaderDepr.Interpret, DisplayIndex = 2 });

            grdInfo.Columns[tagTab.TabHeaderDepr.Interpret].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }
        private void SetFieldMember()
        {
            txtInterpret.FieldMember = tagTab.TabHeaderDepr.Interpret;
            txtTabNo.FieldMember = tagTab.TabHeaderDepr.TabNo;
            dtTabDate.FieldMember = tagTab.TabHeaderDepr.TabDate;
        }
        private void ShowDetialData(XmlDataDocument xmlDoc)
        {
            XmlElement eHeader = XmlProcess.GetVchHeaderElm(xmlDoc);

            foreach (Control ctr in GetListControlsOnForm())
                if (ctr is Aulac.Client.Global.Interface.IAulacValueDataCtrl)
                    ((Aulac.Client.Global.Interface.IAulacValueDataCtrl)ctr).SetValueToControl(eHeader);

            XmlNodeList nodeList = XmlProcess.GetListVchLineItem(xmlDoc);
            grdLine.AddDataToControl(nodeList);
        }
        private List<IAulacDataControl> GetListControlsOnForm()
        {
            List<IAulacDataControl> mListControl = null;
            GetListAulacDataControls(ref mListControl, grpParner);
            mListControl.AddRange(new List<IAulacDataControl> { txtTabNo, dtTabDate });
            return mListControl;
        }

        private XmlDataDocument GetValueToXml()
        {
            XmlDataDocument xmlDoc = CreateXmlDocument("", "");
            if (this.TaskWorking == TaskWorks.Priting)
            {
                XmlElement eHeader = XmlProcess.AddVchHeaderElm(ref xmlDoc);

                XmlElement eVLines = XmlProcess.AddVchLinesElm(ref xmlDoc);
                ((IAulacDataContainerCtrl)grdLine).GetDataToChildNodeXml(eVLines);
            }
            return xmlDoc;
        }
        #endregion

        #region Properties
        private string _TabDeprPrkID = "";
        public string TabDeprPrkID 
        {
            get
            {
                return _TabDeprPrkID;
            }
            set { _TabDeprPrkID = value; }
        }
        #endregion
    }
}
